home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / genarch.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.7 KB  |  278 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: genarch.c,v 1.2 2000/09/19 19:00:23 lpd Exp $ */
  20. /* Generate a header file (arch.h) with parameters */
  21. /* reflecting the machine architecture and compiler characteristics. */
  22.  
  23. #include "stdpre.h"
  24. #include <ctype.h>
  25. #include <stdio.h>
  26. /*
  27.  * In theory, not all systems provide <string.h>, or declare memset in
  28.  * that header file, but at this point I don't think we care about any
  29.  * that don't.
  30.  */
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. /* We should write the result on stdout, but the original Turbo C 'make' */
  35. /* can't handle output redirection (sigh). */
  36.  
  37. private void
  38. section(FILE * f, const char *str)
  39. {
  40.     fprintf(f, "\n\t /* ---------------- %s ---------------- */\n\n", str);
  41. }
  42.  
  43. private clock_t
  44. time_clear(char *buf, int bsize, int nreps)
  45. {
  46.     clock_t t = clock();
  47.     int i;
  48.  
  49.     for (i = 0; i < nreps; ++i)
  50.     memset(buf, 0, bsize);
  51.     return clock() - t;
  52. }
  53.  
  54. private void
  55. define(FILE *f, const char *str)
  56. {
  57.     fprintf(f, "#define %s ", str);
  58. }
  59.  
  60. private void
  61. define_int(FILE *f, const char *str, int value)
  62. {
  63.     fprintf(f, "#define %s %d\n", str, value);
  64. }
  65.  
  66. const char ff_str[] = "ffffffffffffffff";    /* 8 bytes */
  67.  
  68. int
  69. main(int argc, char *argv[])
  70. {
  71.     char *fname = argv[1];
  72.     long one = 1;
  73.     int ff_strlen = sizeof(ff_str) - 1;
  74.     struct {
  75.     char c;
  76.     short s;
  77.     } ss;
  78.     struct {
  79.     char c;
  80.     int i;
  81.     } si;
  82.     struct {
  83.     char c;
  84.     long l;
  85.     } sl;
  86.     struct {
  87.     char c;
  88.     char *p;
  89.     } sp;
  90.     struct {
  91.     char c;
  92.     float f;
  93.     } sf;
  94.     struct {
  95.     char c;
  96.     double d;
  97.     } sd;
  98.     static int log2s[17] =
  99.     {0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4};
  100.     long lm1 = -1;
  101.     long lr1 = lm1 >> 1, lr2 = lm1 >> 2;
  102.     unsigned long um1 = ~(unsigned long)0;
  103.     int im1 = -1;
  104.     int ir1 = im1 >> 1, ir2 = im1 >> 2;
  105.     union {
  106.     long l;
  107.     char *p;
  108.     } pl0, pl1;
  109.     int ars;
  110.     int lwidth = size_of(long) * 8;
  111.     union {
  112.     float f;
  113.     int i;
  114.     long l;
  115.     } f0 , f1, fm1;
  116.     int floats_are_IEEE;
  117.     FILE *f = fopen(fname, "w");
  118.  
  119.     if (f == NULL) {
  120.     fprintf(stderr, "genarch.c: can't open %s for writing\n", fname);
  121.     return exit_FAILED;
  122.     }
  123.     fprintf(f, "/* Parameters derived from machine and compiler architecture */\n");
  124.     /* We have to test the size dynamically here, */
  125.     /* because the preprocessor can't evaluate sizeof. */
  126.     f0.f = 0.0, f1.f = 1.0, fm1.f = -1.0;
  127.     floats_are_IEEE =
  128.     (size_of(float) == size_of(int) ?
  129.      f0.i == 0 && f1.i == (int)0x3f800000 && fm1.i == (int)0xbf800000 :
  130.      f0.l == 0 && f1.l == 0x3f800000L && fm1.l == 0xbf800000L);
  131.  
  132.     section(f, "Scalar alignments");
  133.  
  134. #define OFFSET_IN(s, e) (int)((char *)&s.e - (char *)&s)
  135.     define_int(f, "ARCH_ALIGN_SHORT_MOD", OFFSET_IN(ss, s));
  136.     define_int(f, "ARCH_ALIGN_INT_MOD", OFFSET_IN(si, i));
  137.     define_int(f, "ARCH_ALIGN_LONG_MOD", OFFSET_IN(sl, l));
  138.     define_int(f, "ARCH_ALIGN_PTR_MOD", OFFSET_IN(sp, p));
  139.     define_int(f, "ARCH_ALIGN_FLOAT_MOD", OFFSET_IN(sf, f));
  140.     define_int(f, "ARCH_ALIGN_DOUBLE_MOD", OFFSET_IN(sd, d));
  141. #undef OFFSET_IN
  142.  
  143.     section(f, "Scalar sizes");
  144.  
  145.     define_int(f, "ARCH_LOG2_SIZEOF_SHORT", log2s[size_of(short)]);
  146.     define_int(f, "ARCH_LOG2_SIZEOF_INT", log2s[size_of(int)]);
  147.     define_int(f, "ARCH_LOG2_SIZEOF_LONG", log2s[size_of(long)]);
  148.     define_int(f, "ARCH_SIZEOF_PTR", size_of(char *));
  149.     define_int(f, "ARCH_SIZEOF_FLOAT", size_of(float));
  150.     define_int(f, "ARCH_SIZEOF_DOUBLE", size_of(double));
  151.     if (floats_are_IEEE) {
  152.     define_int(f, "ARCH_FLOAT_MANTISSA_BITS", 24);
  153.     define_int(f, "ARCH_DOUBLE_MANTISSA_BITS", 53);
  154.     } else {
  155.     /*
  156.      * There isn't any general way to compute the number of mantissa
  157.      * bits accurately, especially if the machine uses hex rather
  158.      * than binary exponents.  Use conservative values, assuming
  159.      * the exponent is stored in a 16-bit word of its own.
  160.      */
  161.     define_int(f, "ARCH_FLOAT_MANTISSA_BITS", sizeof(float) * 8 - 17);
  162.     define_int(f, "ARCH_DOUBLE_MANTISSA_BITS", sizeof(double) * 8 - 17);
  163.     }
  164.  
  165.     section(f, "Unsigned max values");
  166.  
  167. #define PRINT_MAX(str, typ, tstr, l)\
  168.   define(f, str);\
  169.   fprintf(f, "((%s)0x%s%s + (%s)0)\n",\
  170.     tstr, ff_str + ff_strlen - size_of(typ) * 2, l, tstr)
  171.     PRINT_MAX("ARCH_MAX_UCHAR", unsigned char, "unsigned char", "");
  172.     PRINT_MAX("ARCH_MAX_USHORT", unsigned short, "unsigned short", "");
  173.     /*
  174.      * For uint and ulong, a different approach is required to keep gcc
  175.      * with -Wtraditional from spewing out pointless warnings.
  176.      */
  177.     define(f, "ARCH_MAX_UINT");
  178.     fprintf(f, "((unsigned int)~0 + (unsigned int)0)\n");
  179.     define(f, "ARCH_MAX_ULONG");
  180.     fprintf(f, "((unsigned long)~0L + (unsigned long)0)\n");
  181. #undef PRINT_MAX
  182.  
  183.     section(f, "Cache sizes");
  184.  
  185.     /*
  186.      * Determine the primary and secondary cache sizes by looking for a
  187.      * non-linearity in the time required to fill blocks with memset.
  188.      */
  189.     {
  190. #define MAX_BLOCK (1 << 20)
  191.     static char buf[MAX_BLOCK];
  192.     int bsize = 1 << 10;
  193.     int nreps = 1;
  194.     clock_t t = 0;
  195.     clock_t t_eps;
  196.  
  197.     /*
  198.      * Increase the number of repetitions until the time is
  199.      * long enough to exceed the likely uncertainty.
  200.      */
  201.  
  202.     while ((t = time_clear(buf, bsize, nreps)) == 0)
  203.         nreps <<= 1;
  204.     t_eps = t;
  205.     while ((t = time_clear(buf, bsize, nreps)) < t_eps * 10)
  206.         nreps <<= 1;
  207.  
  208.     /*
  209.      * Increase the block size until the time jumps non-linearly.
  210.      */
  211.     for (; bsize <= MAX_BLOCK;) {
  212.         clock_t dt = time_clear(buf, bsize, nreps);
  213.  
  214.         if (dt > t + (t >> 1)) {
  215.         t = dt;
  216.         break;
  217.         }
  218.         bsize <<= 1;
  219.         nreps >>= 1;
  220.         if (nreps == 0)
  221.         nreps = 1, t <<= 1;
  222.     }
  223.     define_int(f, "ARCH_CACHE1_SIZE", bsize >> 1);
  224.     /*
  225.      * Do the same thing a second time for the secondary cache.
  226.      */
  227.     if (nreps > 1)
  228.         nreps >>= 1, t >>= 1;
  229.     for (; bsize <= MAX_BLOCK;) {
  230.         clock_t dt = time_clear(buf, bsize, nreps);
  231.  
  232.         if (dt > t * 1.25) {
  233.         t = dt;
  234.         break;
  235.         }
  236.         bsize <<= 1;
  237.         nreps >>= 1;
  238.         if (nreps == 0)
  239.         nreps = 1, t <<= 1;
  240.     }
  241.     define_int(f, "ARCH_CACHE2_SIZE", bsize >> 1);
  242.     }
  243.  
  244.     section(f, "Miscellaneous");
  245.  
  246.     define_int(f, "ARCH_IS_BIG_ENDIAN", 1 - *(char *)&one);
  247.     pl0.l = 0;
  248.     pl1.l = -1;
  249.     define_int(f, "ARCH_PTRS_ARE_SIGNED", (pl1.p < pl0.p));
  250.     define_int(f, "ARCH_FLOATS_ARE_IEEE", (floats_are_IEEE ? 1 : 0));
  251.  
  252.     /*
  253.      * There are three cases for arithmetic right shift:
  254.      * always correct, correct except for right-shifting a long by 1
  255.      * (a bug in some versions of the Turbo C compiler), and
  256.      * never correct.
  257.      */
  258.     ars = (lr2 != -1 || ir1 != -1 || ir2 != -1 ? 0 :
  259.        lr1 != -1 ? 1 :    /* Turbo C problem */
  260.        2);
  261.     define_int(f, "ARCH_ARITH_RSHIFT", ars);
  262.     /*
  263.      * Some machines can't handle a variable shift by
  264.      * the full width of a long.
  265.      */
  266.     define_int(f, "ARCH_CAN_SHIFT_FULL_LONG", um1 >> lwidth == 0);
  267.     /*
  268.      * Determine whether dividing a negative integer by a positive one
  269.      * takes the floor or truncates toward zero.
  270.      */
  271.     define_int(f, "ARCH_DIV_NEG_POS_TRUNCATES", im1 / 2 == 0);
  272.  
  273. /* ---------------- Done. ---------------- */
  274.  
  275.     fclose(f);
  276.     return exit_OK;
  277. }
  278.